package cn.zindesign.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * Http工具类
 * 集成http各个方面的功能方法
 * @author lihui
 * 2012-1-19
 */
public class H {

	/**
	 * 将封装在HashMap中的值输出打印
	 * @param map
	 * @return
	 */
	public static void printParamsListFromMap(Map<String, Object> map){
		if(map==null){
			return ;
		}
		Iterator<?> it = map.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry entry = (Map.Entry) it.next();
	        String key = (String) entry.getKey();
	        String value = String.valueOf(entry
	        .getValue()); 
	        System.out.println("key="+key+",value="+value);
	        //list.add(new BasicNameValuePair(key,value));
	    }
		return ;
	}
	
	/**
	 * null转""
	 * by lihui in Jan 25, 2012 4:34:49 PM
	 * @param str
	 * @return
	 */
	public static String removeNull(String str){
		if(str==null)
			return "";
		else
			return str;
	}
	
	
	/**
	 * http get参数 iso 转 utf-8
	 * by lihui in Jan 25, 2012 4:37:49 PM
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getParamValue(HttpServletRequest request, String name) {
		String result = request.getParameter(name);
		if (result == null) {
			result = "";
		}
		if (S.getEncoding(result).equals("ISO-8859-1")) {
			result = isoToUTF8(result);
		}
		return result;
	}
	
	/**
	 * http get参数 iso 转 utf-8
	 * by lihui in Jan 25, 2012 4:35:39 PM
	 * @param str
	 * @return
	 */
	public static String getParamValue(String str) {
		if (str == null) {
			str = "";
		}
		if (S.getEncoding(str).equals("ISO-8859-1")) {
			str = isoToUTF8(str);
		}
		return str;
	}

	/**
	 * http 集合参数获取 比如checkbox的值
	 * by lihui in Jan 25, 2012 4:36:44 PM
	 * @param request
	 * @param name
	 * @return
	 */
	public static String[] getParamValues(HttpServletRequest request,
			String name) {
		String[] temp = new String[] {};
		String[] result = request.getParameterValues(name);
		if (result == null) {
			result = temp;
		}
		return result;
	}
	
	/**
	 * iso转utf-8
	 * by lihui in Jan 25, 2012 4:37:24 PM
	 * 
	 * @param str
	 * @return
	 */
	public static String isoToUTF8(String str) {
		try {
			if (str != null && !str.equals("")) {
				byte[] byteStr = str.getBytes("ISO-8859-1");
				return new String(byteStr, "utf-8");
			} else {
				return "";
			}
		} catch (Exception e) {
			return str;
		}
	}
	
	/**
	 * 获取当前http地址 参数get型地址
	 * by lihui in Jan 25, 2012 8:02:35 PM
	 * & &amp;
	 * @param request
	 * @param isFullUrl
	 * @return
	 */
	public static String getHttpUrl(HttpServletRequest request,boolean isFullUrl){
		StringBuffer sb = new StringBuffer();
		if(isFullUrl){
			sb.append("http://");
			sb.append(request.getRemoteAddr());
			sb.append(":"+request.getLocalPort());
		}
		sb.append(request.getContextPath());
		sb.append(request.getRequestURI());
		if(request.getQueryString()!=null){
			sb.append("?"+request.getQueryString());
		}
//		Map<String, Object> map = request.getParameterMap();
//		
//		if(map!=null){
//			Iterator<?> it = map.entrySet().iterator();
//			int i=0;
//		    while (it.hasNext()) {
//		        Map.Entry entry = (Map.Entry) it.next();
//		        String key = (String) entry.getKey();
//		        String value = entry.getValue().toString(); 
//		        System.out.println("key="+key.toString()+",value="+String.valueOf(value));
//		        if(i==0){
//		        	sb.append(key.toString()+"="+value.toString());
//		        }else{
//		        	sb.append("&"+key.toString()+"="+value.toString());
//		        }
//		 
//		        //list.add(new BasicNameValuePair(key,value));
//		        i++;
//		    }
//		}
		
		
		return H.isoToUTF8(sb.toString().replace("&", "&amp;"));
		
	}
	
}
